package com.jiangman.weather.adapter;

/**
 * Created by secoro on 2017/4/26.
 */

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiangman.weather.R;
import com.jiangman.weather.bean.WeatherForecast;

import java.util.List;

import util.WeatherUtils;

/**
 * 自定义天气预报listview适配器
 */
public class MyForecastBaseAdapter extends BaseAdapter {
    Activity mActivity;
    /**
     * 用于存放未来天气预报的集合
     */
    private List<WeatherForecast> mForecastList;

    public MyForecastBaseAdapter(Activity mActivity, List<WeatherForecast> mForecastList) {
        this.mActivity = mActivity;
        this.mForecastList = mForecastList;
    }

    @Override
    public int getCount() {
        return mForecastList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ForecastViewHolder forecastViewHolder;
        if (convertView == null) {
            view = View.inflate(mActivity, R.layout.item_forecast, null);
            forecastViewHolder = new ForecastViewHolder();
            forecastViewHolder.tv_date = (TextView) view.findViewById(R.id.tv_date);
            forecastViewHolder.iv_dayType = (ImageView) view.findViewById(R.id.iv_dayType);
            forecastViewHolder.tv_dayType = (TextView) view.findViewById(R.id.tv_dayType);
            forecastViewHolder.tv_tempRange = (TextView) view.findViewById(R.id.tv_tempRange);
            forecastViewHolder.iv_rightArrow = (ImageView) view.findViewById(R.id.iv_rightArrow);
            forecastViewHolder.iv_rightArrow.setImageResource(R.mipmap.ic_right_arrow_white);
            view.setTag(forecastViewHolder);
        } else {
            view = convertView;
            forecastViewHolder = (ForecastViewHolder) view.getTag();
        }


        if (position == 0) {
            forecastViewHolder.tv_date.setText("今天");
        } else if (position == 1) {
            forecastViewHolder.tv_date.setText("明天");
        } else if (position == 2) {
            forecastViewHolder.tv_date.setText("后天");
        } else {
            forecastViewHolder.tv_date.setText(mForecastList.get(position).getWeek());
        }
        String dayType = mForecastList.get(position).getWeatherStart();

        forecastViewHolder.tv_dayType.setText(dayType);
        int iconResId = WeatherUtils.getWhiteIconIdByTypeName(dayType);
        if (iconResId != 0) {
            forecastViewHolder.iv_dayType.setImageResource(iconResId);
            forecastViewHolder.iv_dayType.setVisibility(View.VISIBLE);
        } else {
            forecastViewHolder.iv_dayType.setVisibility(View.INVISIBLE);
        }

        String tempMin = mForecastList.get(position).getTempMin();
        String tempMax = mForecastList.get(position).getTempMax();
        forecastViewHolder.tv_tempRange.setText(tempMin + " ° ~ " + tempMax + " °C");
        return view;
    }


    static class ForecastViewHolder {
        TextView tv_date;
        ImageView iv_dayType;
        TextView tv_dayType;
        TextView tv_tempRange;
        ImageView iv_rightArrow;
    }
}